package com.gopay.scheduler.util;



import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.gopay.common.constants.ep.EpBankConstants;
import com.gopay.common.domain.ep.EpCardBindInfo;
import com.gopay.common.util.Security3DES;


/**
 * 
 * @EpCardUtil.java
 * @author dongdh  
 * @2016年3月2日 上午11:54:23  www.gopay.com.cn Inc.All rights reserved.
 * add by zhaojunting  copy cps 中epcard 的加解密工具类
 */
public final class EpCardSecurityUtil {
    
    /**
     * 单字段加密
     * @param str
     * @return
     */
    public static String encrypt(String str) {
        
        if (StringUtils.isNotEmpty(str)) {
            if (!str.startsWith(EpBankConstants.ENCRPYT_MARK)) {
                byte[] bs = Security3DES.encryptBase64(EpBankConstants.keyBytes, str.getBytes());
                str = EpBankConstants.ENCRPYT_MARK + new String(bs);
            }
        }
        return str;
    }
    
    /**
     * 加密
     * @param obj
     */
    public static void encrypt(EpCardBindInfo info) {
        
        if (info == null) {
            return;
        }

        info.setCertNo(encrypt(info.getCertNo()));
        info.setBankAcctNum(encrypt(info.getBankAcctNum()));
        info.setCreditCardExpired(encrypt(info.getCreditCardExpired()));
    }
    
    /**
     * 单字段解密
     * @param sr
     * @return
     */
    public static String decrypt(String str) {
        
        if (StringUtils.isNotEmpty(str)) {
            if (str.startsWith(EpBankConstants.ENCRPYT_MARK)) {
                str = str.replace(EpBankConstants.ENCRPYT_MARK, "");
                byte[] certNoByte = Security3DES.decryptBase64(EpBankConstants.keyBytes, str.getBytes());
                str = new String(certNoByte);
            }
        }
        
        return str;
    }
    
    /**
     * 解密
     * @param obj
     */
    public static EpCardBindInfo decrypt(EpCardBindInfo info) {
        
        if (info == null) {
            return info;
        }

        info.setCertNo(decrypt(info.getCertNo()));
        info.setBankAcctNum(decrypt(info.getBankAcctNum()));
        info.setCreditCardExpired(decrypt(info.getCreditCardExpired()));
        
        return info;
    }
    
    /**
     * 列表数据解密
     * @param infoList
     * @return
     */
    public static List<EpCardBindInfo> decryptList(List<EpCardBindInfo> infoList) {
        
        if (infoList == null) {
            return null;
        }

        for (EpCardBindInfo info : infoList) {
             decrypt(info);
        }

        return infoList;
    }
    
    
}

